﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.ScheduleView;
using FIFP.Common;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using Telerik.Windows.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using FIFP.DashBoard.Helper;

namespace FIFP.DashBoard.Model
{
    public class FIFPAppointment : Appointment, INotifyPropertyChanged
    {
        #region Variables
        private int planID;
        private int unitID;
        private string division;
        private string unitName;
        private string unitType;
        private string activityType;
        private string activityName;
        private bool isSubjectEnabled;
        private bool isDescEnabled;
        private DateTime originalStart;
        private DateTime originalEnd;
        private DateTime modifiedStart;
        private DateTime modifiedEnd;
        private string fluidType;
        private string modificationReason;
        private string lsdReason;
        private string optimizedReason;
        private bool isOverlapped;
        private bool isAppliedSimops;
        private bool isAppliedLSDSimops;
        private bool isAppliedCrossingLine;
        private bool isSelectedForCancelSimops;
        private bool isBypassMOL;

        private int scenarioId = -1;
        private string scenarioName;
        private string scenarioDescription;

        private bool isAppointmentDelete = false;
        private string unitStatus;
        private string activityCode;
        private bool isLinkedShutdownPlan = false;//Linked shutdowns of default unit shutdown - LsdPlans
        private bool isDefaultUnitShutdown = false;//Default unit shutdown - Selected Activity

        private List<FIFPAppointment> lsdPlans = new List<FIFPAppointment>();
        private ObservableCollection<FIFPAppointment> filteredLsdPlans = new ObservableCollection<FIFPAppointment>();
        private string selectedString = FIFPConstants.SHUTDOWN_CLOSEDSTRING_ALLSTRINGS;
        private int activityLevel = 1;

        public int Overlap_Id { get; set; }
        public HashSet<int> OverlapIdSet = new HashSet<int>();
        public int Overlap_Modification_Id { get; set; }
        public int nIndex { get; set; }
        public Visibility RadioButtonVisibility { get; set; }
        public Visibility SIMOPSRadioButtonVisibility { get; set; }
        private double capacityPercentage = 1.0;
        public double ReducedCapacity { get; set; }
        public string SelectedOilString { get; set; }
        public string LSDDeletedIds { get; set; }
        public string CombinedTowerLSDType { get; set; }
        public string Remarks { get; set; }
        private int isModified = -1;
        public int ActivityId { get; set; }
        public int DivisionId { get; set; }
        private bool isSelectedLocationActivityLock = false;
        public int PlanActivityId { get; set; }
        public string ParentActivityCode { get; set; }
        public string ActivityCodeDescription { get; set; }
        public bool IsRadialSwitch { get; set; }        
        public int DB_OverlapId { get; set; }
        public bool IsRDVisibility { get; set; }
        public string DB_Remarks { get; set; }
        public string Description { get; set; }
        public string modifiedTotalDays;
        public string originalTotalDays;
        public int LSDParentActivityPlanId { get; set; }
        public bool IsOverlapRemoved { get; set; }
        public int LSDRCParentUnitId { get; set; }
        #endregion

        #region Properties
        public int PlanID
        {
            get { return planID; }
            set
            {
                if (planID != value)
                {
                    planID = value;
                    OnPropertyChanged(() => this.planID);
                }
            }
        }

        public int UnitID
        {
            get { return unitID; }
            set
            {
                if (unitID != value)
                {
                    unitID = value;
                    OnPropertyChanged(() => this.unitID);
                }
            }
        }
        public string Division
        {
            get { return division; }
            set
            {
                if (division != value)
                {
                    division = value;
                    OnPropertyChanged(() => this.division);
                }
            }
        }

        public string UnitName
        {
            get { return unitName; }
            set
            {
                if (unitName != value)
                {
                    unitName = value;
                    OnPropertyChanged(() => this.unitName);
                }
            }
        }

        public string UnitType
        {
            get { return unitType; }
            set
            {
                if (unitType != value)
                {
                    unitType = value;
                    OnPropertyChanged(() => this.unitType);
                }
            }
        }

        public string ActivityName
        {
            get { return activityName; }
            set
            {
                if (activityName != value)
                {
                    activityName = value;
                    OnPropertyChanged(() => this.activityName);
                }
            }
        }

        public string ActivityType
        {
            get { return activityType; }
            set
            {
                if (activityType != value)
                {
                    activityType = value;
                    OnPropertyChanged(() => this.activityType);
                }
            }
        }

        public int ActivityLevel
        {
            get { return activityLevel; }
            set
            {
                if (activityLevel != value)
                {
                    activityLevel = value;
                    OnPropertyChanged(() => this.activityLevel);
                }
            }
        }


        public bool IsSubjectEnabled
        {
            get { return isSubjectEnabled; }
            set
            {
                if (isSubjectEnabled != value)
                {
                    isSubjectEnabled = value;
                    OnPropertyChanged(() => this.isSubjectEnabled);
                }
            }
        }

        public bool IsDescEnabled
        {
            get { return isDescEnabled; }
            set
            {
                if (isDescEnabled != value)
                {
                    isDescEnabled = value;
                    OnPropertyChanged(() => this.isDescEnabled);
                }
            }
        }

        public DateTime OriginalStart
        {
            get { return originalStart; }
            set
            {
                if (originalStart != value)
                {
                    originalStart = value;
                    OnPropertyChanged(() => this.originalStart);
                }
            }
        }

        public DateTime OriginalEnd
        {
            get { return originalEnd; }
            set
            {
                if (originalEnd != value)
                {
                    originalEnd = value;
                    OnPropertyChanged(() => this.originalEnd);
                }
            }
        }

        public DateTime ModifiedStart
        {
            get { return modifiedStart; }
            set
            {
                if (modifiedStart != value)
                {
                    modifiedStart = value;
                    //OnPropertyChanged(() => this.modifiedStart);
                    onPropertyChanged(this, "ModifiedStart");
                }
            }
        }

        public DateTime ModifiedEnd
        {
            get { return modifiedEnd; }
            set
            {
                if (modifiedEnd != value)
                {
                    modifiedEnd = value;
                    //OnPropertyChanged(() => this.modifiedEnd);
                    onPropertyChanged(this, "ModifiedEnd");
                }
            }
        }

        public override DateTime Start
        {
            get { return base.Start; }
            set
            {
                base.Start = value;
                onPropertyChanged(this, "Start");
            }
        }

        public override DateTime End
        {
            get { return base.End; }
            set
            {
                base.End = value;
                onPropertyChanged(this, "End");
            }
        }

        public string ModifiedTotalDays
        {
            get 
            {
                int days = Convert.ToInt32(Math.Round(base.End.Subtract(base.Start).TotalDays));
                return days > 1 ? days.ToString() + " Days" : days.ToString() + " Day"; 
            }
            set
            {
                if (modifiedTotalDays != value)
                {
                    modifiedTotalDays = value;
                    onPropertyChanged(this, "ModifiedTotalDays");
                    //OnPropertyChanged("ModifiedTotalDays");
                }
            }
        }

        public string OriginalTotalDays
        {
            get 
            {
                int days = Convert.ToInt32(Math.Round(OriginalEnd.Subtract(OriginalStart).TotalDays));
                return days > 1 ? days.ToString() + " Days" : days.ToString() + " Day"; 
            }
            set
            {
                if (originalTotalDays != value)
                {
                    originalTotalDays = value;
                    onPropertyChanged(this, "OriginalTotalDays");
                    //OnPropertyChanged("OriginalTotalDays");
                }
            }
        }       

        public string FluidType
        {
            get { return fluidType; }
            set
            {
                if (fluidType != value)
                {
                    fluidType = value;
                    OnPropertyChanged(() => this.fluidType);
                }
            }
        }

        public string ModificationReason
        {
            get { return modificationReason; }
            set
            {
                if (modificationReason != value)
                {
                    modificationReason = value;
                    this.OnPropertyChanged("ModificationReason");
                }
            }
        }

        public string LsdReason
        {
            get { return lsdReason; }
            set
            {
                if (lsdReason != value)
                {
                    lsdReason = value;
                    this.OnPropertyChanged("LsdReason");
                }
            }
        }

        public string OptimizedReason
        {
            get { return optimizedReason; }
            set
            {
                if (optimizedReason != value)
                {
                    optimizedReason = value;
                    this.OnPropertyChanged("OptimizedReason");
                }
            }
        }

        public bool IsOverlapped
        {
            get { return isOverlapped; }
            set
            {
                if (isOverlapped != value)
                {
                    isOverlapped = value;
                    this.OnPropertyChanged("IsOverlapped");
                }
            }
        }

        public string SelectedString
        {
            get { return selectedString; }
            set
            {
                if (selectedString != value)
                {
                    selectedString = value;
                    onPropertyChanged(this, "SelectedString");
                }
            }
        }

        public bool IsAppliedSimops
        {
            get { return isAppliedSimops; }
            set
            {
                if (isAppliedSimops != value)
                {
                    isAppliedSimops = value;
                    this.OnPropertyChanged("IsAppliedSimops");
                }
            }
        }

        public bool IsAppliedLSDSimops
        {
            get { return isAppliedLSDSimops; }
            set
            {
                if (isAppliedLSDSimops != value)
                {
                    isAppliedLSDSimops = value;
                    this.OnPropertyChanged("IsAppliedLSDSimops");
                }
            }
        }

        public bool IsAppliedCrossingLine
        {
            get { return isAppliedCrossingLine; }
            set
            {
                if (isAppliedCrossingLine != value)
                {
                    isAppliedCrossingLine = value;
                    this.OnPropertyChanged("IsAppliedCrossingLine");
                }
            }
        }


        public bool IsSelectedForCancelSimops
        {
            get { return isSelectedForCancelSimops; }
            set
            {
                if (isSelectedForCancelSimops != value)
                {
                    isSelectedForCancelSimops = value;
                    this.OnPropertyChanged("IsSelectedForCancelSimops");
                }
            }
        }

        public bool IsBypassMOL
        {
            get { return isBypassMOL; }
            set
            {
                if (isBypassMOL != value)
                {
                    isBypassMOL = value;
                    this.OnPropertyChanged("IsBypassMOL");
                }
            }
        }

        public int ScenarioId
        {
            get { return scenarioId; }
            set
            {
                if (scenarioId != value)
                {
                    scenarioId = value;
                    OnPropertyChanged(() => this.scenarioId);
                }
            }
        }

        public string ScenarioName
        {
            get { return scenarioName; }
            set
            {
                if (scenarioName != value)
                {
                    scenarioName = value;
                    OnPropertyChanged(() => this.scenarioName);
                }
            }
        }

        public string ScenarioDescription
        {
            get { return scenarioDescription; }
            set
            {
                if (scenarioDescription != value)
                {
                    scenarioDescription = value;
                    OnPropertyChanged(() => this.scenarioDescription);
                }
            }
        }

        public bool IsAppointmentDelete
        {
            get { return isAppointmentDelete; }
            set
            {
                if (isAppointmentDelete != value)
                {
                    isAppointmentDelete = value;
                    this.OnPropertyChanged("IsAppointmentDelete");
                }
            }
        }

        public string UnitStatus
        {
            get { return unitStatus; }
            set
            {
                if (unitStatus != value)
                {
                    unitStatus = value;
                    onPropertyChanged(this, "UnitStatus");
                }
            }
        }

        public string ActivityCode
        {
            get { return activityCode; }
            set
            {
                if (activityCode != value)
                {
                    activityCode = value;
                    this.OnPropertyChanged("ActivityCode");
                }
            }
        }

        public bool IsDefaultUnitShutdown
        {
            get { return isDefaultUnitShutdown; }
            set
            {
                if (isDefaultUnitShutdown != value)
                {
                    isDefaultUnitShutdown = value;
                    this.OnPropertyChanged("IsDefaultUnitShutdown");
                }
            }
        }

        public bool IsLinkedShutdownPlan
        {
            get { return isLinkedShutdownPlan; }
            set
            {
                if (isLinkedShutdownPlan != value)
                {
                    isLinkedShutdownPlan = value;
                    this.OnPropertyChanged("IsLinkedShutdownPlan");
                }
            }
        }
        public bool IsSelectedLocationActivityLock
        {
            get { return isSelectedLocationActivityLock; }
            set
            {
                if (isSelectedLocationActivityLock != value)
                {
                    isSelectedLocationActivityLock = value;
                    this.OnPropertyChanged("IsSelectedLocationActivityLock");
                }
            }
        }

        public List<FIFPAppointment> LsdPlans
        {
            get { return lsdPlans; }
            set
            {
                if (lsdPlans != value)
                {
                    lsdPlans = value;
                    this.OnPropertyChanged("LsdPlans");
                }
            }
        }

        public ObservableCollection<FIFPAppointment> FilteredLsdPlans
        {
            get { return filteredLsdPlans; }
            set
            {
                if (filteredLsdPlans != value)
                {
                    filteredLsdPlans = value;
                    this.OnPropertyChanged("FilteredLsdPlans");
                }
            }
        }

        public double CapacityPercentage
        {
            get { return capacityPercentage; }
            set
            {
                if (capacityPercentage != value)
                {
                    capacityPercentage = value;
                    this.OnPropertyChanged("CapacityPercentage");
                }
            }
        }

        public int IsModified
        {
            get { return isModified; }
            set
            {
                if (isModified != value)
                {
                    isModified = value;
                    this.OnPropertyChanged("IsModified");
                }
            }
        }

        #endregion

        #region Constructor
        public FIFPAppointment()
        {

        }
        #endregion

        #region Override Methods
        public override IAppointment Copy()
        {
            var plan = new FIFPAppointment();
            plan.CopyFrom(this);
            return plan;
        }

        public override void CopyFrom(IAppointment other)
        {
            var plan = other as FIFPAppointment;
            if (plan != null)
            {
                base.CopyFrom(other);
            }
        }
        #endregion

        // Declare the PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;

        // OnPropertyChanged will raise the PropertyChanged event passing the source property that is being updated.
        private void onPropertyChanged(object sender, string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    #region FIFPSlot class
    public class FIFPSlot : Slot
    {
        public string ImageSource { get; set; }
        public string Description { get; set; }

        public FIFPSlot(DateTime start, DateTime end)
            : base(start, end)
        {

        }

        public override Slot Copy()
        {
            Slot slot = new FIFPSlot(this.Start, this.End);
            slot.CopyFrom(this);
            return slot;
        }

        public override void CopyFrom(Slot other)
        {
            var otherSlot = other as FIFPSlot;
            if (otherSlot != null)
            {
                this.ImageSource = otherSlot.ImageSource;
                this.Description = otherSlot.Description;
                base.CopyFrom(otherSlot);
            }
        }
    }
    #endregion

    #region FIFPResource class
    public class FIFPResource : Resource
    {
        public FIFPResource()
        {

        }

        public FIFPResource(string name, string type, string icon)
            : base(name, type)
        {
            Icon = icon;
        }

        public FIFPResource(FIFPAppointment appointment)
        {
            this.ResourceType = "Speaker";
            this.ResourceName = appointment.Resources[0].ResourceName;
            this.DisplayName = appointment.Resources[0].DisplayName;
        }
        public string Icon { get; set; }

        public string SchedulerColor { get; set; }

        public BitmapImage UserPhoto { get; set; }
    }
    #endregion
}